﻿//Author: Ha Bao Minh Ky
// Group : 06TH1d.12
// Created : 12/5/2010
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data.Common;

namespace QUANLYNHASACH.App_code.BLL    
{
    public class NXB_BLL
    {
        public List<QUANLYNHASACH.NXB_td> LayDanhSachNXB()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "getNhaXuatBan";
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataReader reader = cmd.ExecuteReader();

                    List<QUANLYNHASACH.NXB_td> nxbLst = new List<QUANLYNHASACH.NXB_td>();

                    while (reader.Read())
                    {
                        QUANLYNHASACH.NXB_td  nxb = new QUANLYNHASACH.NXB_td();
                        nxb._idNhaXuatBan  = reader[0].ToString();
                        nxb._TenNXB  = reader[1].ToString();
                        nxbLst.Add(nxb);
                    }
                    conn.Close();
                    return nxbLst;
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra: " + ex.Message, "Thông báo lỗi");
            }
            return null;
        }
        

        ////////////////
        public void CapNhatNXB(string idNhaXuatBan, string TenNXB, string DiaChi, string NguoiDaiDien, string SoDienThoai, string Email)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "editNhaXuatBan";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idNhaXuatBan";
                    param.Value = idNhaXuatBan ;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TenNXB_new";
                    param.Value = TenNXB ;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@DiaChi_new";
                    param.Value = DiaChi;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@NguoiDaiDien_new";
                    param.Value = NguoiDaiDien;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@SoDienThoai_new";
                    param.Value = SoDienThoai;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@Email_new";
                    param.Value = Email;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
        ///////////////
        public void  ThemNXB(string idNhaXuatBan, string TenNXB, string DiaChi, string NguoiDaiDien, string SoDienThoai, string Email)
        {
           // bool ketqua = false;
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.connect.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "addNhaXuatBan";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idNhaXuatBan";
                    param.Value = idNhaXuatBan;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@TenNXB";
                    param.Value = TenNXB;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@DiaChi";
                    param.Value = DiaChi;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@NguoiDaiDien";
                    param.Value = NguoiDaiDien;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@SoDienThoai";
                    param.Value = SoDienThoai;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@Email";
                    param.Value = Email;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();
                    ketqua = true;
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
               // ketqua = false;
            }
           // return ketqua;
        }
        //////////////////////////////
        public void XoaNXB(string idNhaXuatBan)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delNhaXuatBan";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idNhaXuatBan";
                    param.Value = idNhaXuatBan;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);                                       
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
    }
}
